package com.merlin.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 
* @ClassName: ZhbGatewayApplication 
* @Description: gateway的启动页 
* @owner 张海滨  zhanghb22 
* @author 张海滨  zhanghb22 
* @date 2017年12月22日 下午1:56:09 
*
 */
@EnableZuulProxy
@SpringBootApplication
public class MerlinGatewayApplication
{

	public static void main(String[] args)
	{
		SpringApplication.run(MerlinGatewayApplication.class, args);
	}

	/**
	 * 执行请求前的过滤器
	 * @return
	 */
	@Bean
	public PreFilter simpleFilter()
	{
		return new PreFilter();
	}

	/**
	 * 解决跨域问题的过滤器 
	 */
	@Bean
	public CorsFilter corsFilter()
	{
		final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		final CorsConfiguration config = new CorsConfiguration();
		config.setAllowCredentials(true);
		config.addAllowedOrigin("*");
		config.addAllowedHeader("*");
		config.addAllowedMethod("OPTIONS");
		config.addAllowedMethod("HEAD");
		config.addAllowedMethod("GET");
		config.addAllowedMethod("PUT");
		config.addAllowedMethod("POST");
		config.addAllowedMethod("DELETE");
		config.addAllowedMethod("PATCH");
		source.registerCorsConfiguration("/**", config);
		return new CorsFilter(source);
	}

}
